﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using ApplicationGenerator.CommonModule;
using ApplicationGenerator.DataAccess;

namespace GUserInterface
{
    public enum ProjectType { New, Old };


    public partial class SolutionDlg : Form
    {

        #region Variables

        private ProjectType pType;

        private string ConnectionString = string.Empty;

        #endregion

        #region Construction

        public SolutionDlg(ProjectType type)
        {
            InitializeComponent();
            pType = type;


            this.DialogResult = DialogResult.None;
        }

        #endregion

        #region Methods

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.Empty != textBox1.Text &&
                string.Empty != textBox2.Text &&
                string.Empty != maskedTextBox1.Text &&
                string.Empty != textBox3.Text //&&
                )//string.Empty != textBox4.Text)
            {
                string connectionString = null;

                if (ProjectType.New == pType)
                {
                    connectionString = DesignerDataBaseManager.Instance.PrepareConnectionString(textBox1.Text,
                        textBox2.Text, maskedTextBox1.Text, DesignerDataBaseManager.ConnectionStringType.Creation);
                }
                else
                {
                    connectionString = DesignerDataBaseManager.Instance.PrepareConnectionString(textBox1.Text,
                        textBox2.Text, maskedTextBox1.Text, DesignerDataBaseManager.ConnectionStringType.Load);
                }

                if (null == connectionString)
                {
                    MessageBox.Show("Bad parameters!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (false == DesignerDataBaseManager.Instance.PrepareDatabase(textBox1.Text))
                {
                    MessageBox.Show("Bad parameters!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("not all fields set!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
            {
                textBox4.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        #endregion

        #region Properties

        public string DirPath
        {
            get { return textBox4.Text; }
        }

        public string ModelsDirectoryPath
        {
            get { return textBox3.Text; }
        }

        public string UserName
        {
            get { return textBox2.Text; }
        }

        public string Passwd
        {
            get { return maskedTextBox1.Text; }
        }

        public string DbName
        {
            get { return textBox1.Text; }
        }

        public bool CreateTables
        {
            get { return chbCreateTables.Checked; }
        }

        #endregion

        private void SolutionDlg_Load(object sender, EventArgs e)
        {
            if (ProjectType.Old == pType)
            {
                openFileDialog1.Filter = "Dynamic Link Library|*.xml";
                if (DialogResult.OK == openFileDialog1.ShowDialog())
                {
                    XmlConfigMaker configMaker = new XmlConfigMaker(openFileDialog1.FileName);

                    //DODAC SPRAWDZANIE CZY NIE MA INDEXOUTOFBOUND!!! 

                    try
                    {
                        List<ConfigEntry> connectionString = configMaker.GetEntriesByName("ConnectionString");
                        List<ConfigEntry> asmblyLocString = configMaker.GetEntriesByName("AsmblyLocation");
                        List<ConfigEntry> projectLocation = configMaker.GetEntriesByName("ProjectLocation");
                        List<ConfigEntry> dbName = configMaker.GetEntriesByName("DbName");
                        List<ConfigEntry> userName = configMaker.GetEntriesByName("UserName");
                        List<ConfigEntry> passwd = configMaker.GetEntriesByName("Passwd");

                        if (dbName.Count >= 1 &&
                            userName.Count >= 1 &&
                            passwd.Count >= 1 &&
                            asmblyLocString.Count >= 1 &&
                            projectLocation.Count >= 1)
                        {
                            //add text
                            textBox1.Text = dbName[0].Value;
                            textBox2.Text = userName[0].Value;
                            maskedTextBox1.Text = passwd[0].Value;

                            textBox3.Text = asmblyLocString[0].Value;
                            textBox4.Text = projectLocation[0].Value;
                        }
                        else
                        {
                            throw new Exception();
                        }

                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Error occured during loading project!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.DialogResult = DialogResult.Cancel;
                        this.Close();
                    }

                    //block controls
                    textBox1.Enabled = false;
                    textBox2.Enabled = false;
                    maskedTextBox1.Enabled = false;
                    button3.Enabled = false;
                    button2.Enabled = false;
                }

            }
        }


    }
}
